package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.LevelEntity;
import com.dragon.modules.yuegou.entity.member.LiftConditionsEntity;
import com.dragon.modules.yuegou.entity.member.LiftEntity;
import com.dragon.modules.yuegou.enums.member.LiftConditionsLiftType;
import com.dragon.modules.yuegou.enums.member.LiftType;
import com.dragon.modules.yuegou.mapper.member.LiftMapper;
import com.dragon.modules.yuegou.model.member.Lift;
import com.dragon.modules.yuegou.model.member.LiftConditions;
import com.dragon.modules.yuegou.query.member.LiftConditionsQuery;
import com.dragon.modules.yuegou.query.member.LiftQuery;
import com.dragon.modules.yuegou.query.member.MemLiftConditionsQuery;
import com.dragon.modules.yuegou.service.member.LevelService;
import com.dragon.modules.yuegou.service.member.LiftConditionsService;
import com.dragon.modules.yuegou.service.member.LiftService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员升降表(升降配置) 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:40
 * @version 2.0
 */
@Service("memLiftService")
public class LiftServiceImpl
		extends ServiceImpl<LiftMapper, LiftEntity>
		implements LiftService {

	@Autowired
	private LevelService levelService;

	@Autowired
	private LiftConditionsService liftConditionsService;

	@Transactional
	@Override
	public void saveConditions(List<MemLiftConditionsQuery> querys){
		
		if (CollectionUtils.isNotEmpty(querys)) {
			for (MemLiftConditionsQuery memLiftConditionsQuery : querys) {
				Lift entity = new Lift();
				entity.setDel(true);
				LiftQuery liftQuery = new LiftQuery();
				liftQuery.setLevelId(memLiftConditionsQuery.getLevelId());
				liftQuery.setDel(false);
				List<LiftEntity> list = list(liftQuery.toQueryWrapper());
				//删除条件
				for (LiftEntity lift : list) {
					LiftConditionsQuery conditions = new LiftConditionsQuery();
					conditions.setLiftId(lift.getId());
					liftConditionsService.remove(conditions.toQueryWrapper());
				}
				//逻辑删除规则
				baseMapper.update(entity, liftQuery.toQueryWrapper());
			}
		}
		
		for (MemLiftConditionsQuery query : querys) {
			Check.notNull(query.getLevelId(),"会员等级id不能为空");
			LevelEntity level = levelService.getById(query.getLevelId());
			LiftQuery liftQuery = new LiftQuery();
			liftQuery.setLevelId(query.getLevelId());
			if (query.getType() == 1) {
				liftQuery.setType(LiftType.UPGRADE);
			} else {
				liftQuery.setType(LiftType.DEMOTION);
			}
				
			Lift entity = new Lift();
			entity.setLevelId(query.getLevelId());
			entity.setBusdId(level.getBusdId());
			entity.setSerialNumber(query.getSerialNumber());
			if (query.getType() == 1) {
				entity.setType(LiftType.UPGRADE);
			} else {
				entity.setType(LiftType.DEMOTION);
			}
			save(entity);
			//批量新增条件
			insertBatchConditions(entity.getId(), query.getType(), query.getLiftConditionsList());
		}
	}

	private void insertBatchConditions(String liftId, Integer type, List<LiftConditions> liftConditionsList){
		List<LiftConditionsEntity> conditionsList = new ArrayList<>();
		for (LiftConditions liftConditions : liftConditionsList) {
			liftConditions.setLiftId(liftId);
			if(type.equals(LiftConditionsLiftType.UPGRADE.getCode())){
				liftConditions.setLiftType(LiftConditionsLiftType.UPGRADE);
			}else{
				liftConditions.setLiftType(LiftConditionsLiftType.DEMOTION);
			}
			conditionsList.add(liftConditions);
		}
		liftConditionsService.saveBatch(conditionsList);
	}

	@Override
	public List<Lift> getLift(String id, Integer type){
		Check.notEmpty(id,"会员等级id不能为空");
		Check.notNull(type,"升降类型不能为空");
		LiftQuery query = new LiftQuery();
		query.setLevelId(id);
		if(type.equals(LiftType.UPGRADE.getCode())) {
			query.setType(LiftType.UPGRADE);
		}else{
			query.setType(LiftType.DEMOTION);
		}
		List<Lift> lifts = list(query.toQueryWrapper()).stream().map(Lift::new).collect(Collectors.toList());
		for (Lift lift : lifts) {
			LiftConditionsQuery liftConditionsQuery = new LiftConditionsQuery();
			liftConditionsQuery.setLiftId(lift.getId());
			if(type.equals(LiftType.UPGRADE.getCode())) {
				liftConditionsQuery.setLiftType(LiftConditionsLiftType.UPGRADE);
			}else{
				liftConditionsQuery.setLiftType(LiftConditionsLiftType.DEMOTION);
			}
			List<LiftConditionsEntity> liftConditionsLift = liftConditionsService.list(liftConditionsQuery.toQueryWrapper());
			lift.setLiftConditionsList(liftConditionsLift.stream().map(LiftConditions::new).collect(Collectors.toList()));
		}
		return lifts;
	}

}